package dobest.ray.com.jiongjiong.activity.action.EyeExercises;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import dobest.ray.com.jiongjiong.R;
import dobest.ray.com.jiongjiong.activity.BaseActivity;
import dobest.ray.com.jiongjiong.adapter.EyeExerciseAdapter;
import dobest.ray.com.jiongjiong.utils.ACacheHelper;
import dobest.ray.com.jiongjiong.utils.ShareUtils;
import dobest.ray.com.jiongjiong.views.NavigationBar;

/**
 * Created by Ray on 2016/3/19.
 * https://github.com/ray0807
 * 眼肌操训练
 */
public class EyeExercisesActivity extends BaseActivity implements View.OnClickListener {
    private PullToRefreshListView ptr_list;
    private View header;
    private EyeExerciseAdapter adapter;
    private RelativeLayout rl_eye_exercise_imgae1;
    private RelativeLayout rl_eye_exercise_imgae2;
    private RelativeLayout rl_eye_exercise_imgae3;

    private ImageView iv_eye_exercise_3;
    private TextView tv_eye_exercise_3;
    private ImageView iv_eye_exercise_2;
    private TextView tv_eye_exercise_2;
    private ImageView iv_eye_exercise_1;
    private TextView tv_eye_exercise_1;
    private TextView tv_eye_exercise_time_3;
    private TextView tv_eye_exercise_time_2;
    private TextView tv_eye_exercise_time_1;

    private static final int REFRESH_UI = 0;


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case REFRESH_UI:

                    setRefreshUI(playMax);
                    break;
            }
            super.handleMessage(msg);
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_eye_exercises);
        findViews();
        init();
        addListeners();
    }


    @Override
    public void findViews() {
        navi = (NavigationBar) findViewById(R.id.navi);
        navi.setTitle("眼肌操");
        navi.setBackListener(R.mipmap.img_back, "");
        navi.setImageRight(this, R.mipmap.img_eye_exe_share);

        header = getLayoutInflater().inflate(R.layout.header_eye_exe, null);
        ptr_list = (PullToRefreshListView) findViewById(R.id.ptr_list);

        rl_eye_exercise_imgae1 = (RelativeLayout) header.findViewById(R.id.rl_eye_exercise_imgae1);
        rl_eye_exercise_imgae2 = (RelativeLayout) header.findViewById(R.id.rl_eye_exercise_imgae2);
        rl_eye_exercise_imgae3 = (RelativeLayout) header.findViewById(R.id.rl_eye_exercise_imgae3);
        iv_eye_exercise_3 = (ImageView) header.findViewById(R.id.iv_eye_exercise_3);
        tv_eye_exercise_3 = (TextView) header.findViewById(R.id.tv_eye_exercise_3);
        iv_eye_exercise_2 = (ImageView) header.findViewById(R.id.iv_eye_exercise_2);
        tv_eye_exercise_2 = (TextView) header.findViewById(R.id.tv_eye_exercise_2);
        iv_eye_exercise_1 = (ImageView) header.findViewById(R.id.iv_eye_exercise_1);
        tv_eye_exercise_1 = (TextView) header.findViewById(R.id.tv_eye_exercise_1);
        tv_eye_exercise_time_3 = (TextView) header.findViewById(R.id.tv_eye_exercise_time_3);
        tv_eye_exercise_time_2 = (TextView) header.findViewById(R.id.tv_eye_exercise_time_2);
        tv_eye_exercise_time_1 = (TextView) header.findViewById(R.id.tv_eye_exercise_time_1);
    }

    @Override
    public void init() {
        ptr_list.setMode(PullToRefreshBase.Mode.BOTH);
        adapter = new EyeExerciseAdapter(this);
        ptr_list.setAdapter(adapter);
        ptr_list.getRefreshableView().addHeaderView(header);
    }

    @Override
    public void addListeners() {
        rl_eye_exercise_imgae1.setOnClickListener(this);
        rl_eye_exercise_imgae2.setOnClickListener(this);
        rl_eye_exercise_imgae3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.ll_image_right:
                ShareUtils.showShare(this, null, true);
                break;
            case R.id.rl_eye_exercise_imgae1:
                currentPlay = 1;
                intent = new Intent(EyeExercisesActivity.this, VideoPlayerActivity.class);

                break;
            case R.id.rl_eye_exercise_imgae2:
                if (playMax >= 2) {
                    currentPlay = 2;
                    intent = new Intent(EyeExercisesActivity.this, VideoPlayerActivity.class);
                } else {
                    showToast("请先完成第一节广播体操");
                }

                break;
            case R.id.rl_eye_exercise_imgae3:
                if (playMax >= 3) {
                    currentPlay = 3;
                    intent = new Intent(EyeExercisesActivity.this, VideoPlayerActivity.class);
                } else {
                    showToast("请先完成第二节广播体操");
                }
                break;
        }
        if (intent != null) {

            intent.putExtra("index", playMax);
            switch (currentPlay) {
                case 1:
                    intent.putExtra("videoPath", "http://baobab.wdjcdn.com/1456117847747a_x264.mp4");
                    break;
                case 2:
                    intent.putExtra("videoPath", "http://baobab.wdjcdn.com/1456117847747a_x264.mp4");
                    break;
                case 3:
                    intent.putExtra("videoPath", "http://baobab.wdjcdn.com/1456117847747a_x264.mp4");
                    break;
            }

            startActivity(intent);
        }
    }

    private int playMax = 1;
    private int currentPlay = 1;

    private void setRefreshUI(int playMax) {
        switch (playMax) {
            case 1:
                break;
            case 2:
                tv_eye_exercise_1.setText("第二节:挤按睛明穴");
                iv_eye_exercise_1.setImageResource(R.mipmap.img_e2);
                tv_eye_exercise_time_1.setText("4:10");

                tv_eye_exercise_2.setText("第一节:揉天应穴(攒竹下三分)");
                iv_eye_exercise_2.setImageResource(R.mipmap.img_e1);
                tv_eye_exercise_time_2.setText("5:20");
                break;
            case 3:
                tv_eye_exercise_1.setText("第三节:揉刮眼眶");
                iv_eye_exercise_1.setImageResource(R.mipmap.img_e3);
                tv_eye_exercise_time_1.setText("1:50");

                tv_eye_exercise_2.setText("第一节:揉天应穴(攒竹下三分)");
                iv_eye_exercise_2.setImageResource(R.mipmap.img_e1);
                tv_eye_exercise_time_2.setText("5:20");

                tv_eye_exercise_3.setText("第二节:挤按睛明穴");
                iv_eye_exercise_3.setImageResource(R.mipmap.img_e2);
                tv_eye_exercise_time_3.setText("4:10");

                break;
            default:
                //所有的都播完了
                tv_eye_exercise_3.setText("第三节:揉刮眼眶");
                iv_eye_exercise_3.setImageResource(R.mipmap.img_e3);
                tv_eye_exercise_time_3.setText("1:50");

                tv_eye_exercise_1.setText("第一节:揉天应穴(攒竹下三分)");
                iv_eye_exercise_1.setImageResource(R.mipmap.img_e1);
                tv_eye_exercise_time_1.setText("5:20");

                tv_eye_exercise_2.setText("第二节:挤按睛明穴");
                iv_eye_exercise_2.setImageResource(R.mipmap.img_e2);
                tv_eye_exercise_time_2.setText("4:10");
                break;

        }

    }

    @Override
    protected void onResume() {
        Log.d("wanglei", "cache:" + ACacheHelper.getInstance().getVideoIndex(this));
        if (ACacheHelper.getInstance().getVideoIndex(this) > 0) {
            playMax = ACacheHelper.getInstance().getVideoIndex(this);
            handler.sendEmptyMessage(REFRESH_UI);
        }
        super.onResume();
    }

}
